package com.tidy.seed.admin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tidy.seed.admin.system.dto.AddMenuIdsByRoleIdDTO;
import com.tidy.seed.admin.system.entity.RoleMenu;
import com.tidy.seed.admin.system.service.RoleMenuService;
import com.tidy.seed.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

// ✅ Swagger 引入
import io.swagger.annotations.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HZ-ZY
 * @since 2025-08-06
 */
@RestController
@Api(tags = "RoleMenu 接口")
@RequestMapping("/admin/system/role-menu")
public class RoleMenuController {

    @Resource
    private RoleMenuService roleMenuService;

    @ApiOperation(value = "新增或更新RoleMenu")
    @PostMapping
    public Result save(@RequestBody @ApiParam("RoleMenu 对象") RoleMenu roleMenu) {
        roleMenuService.saveOrUpdate(roleMenu);
        return Result.success();
    }

    @ApiOperation(value = "根据 ID 删除RoleMenu")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable @ApiParam("ID") Integer id) {
        roleMenuService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "批量删除RoleMenu")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody @ApiParam("ID 列表") List<Integer> ids) {
        roleMenuService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation(value = "查询全部RoleMenu")
    @GetMapping
    public Result findAll() {
        return Result.success(roleMenuService.list());
    }

    @ApiOperation(value = "根据 ID 查询RoleMenu")
    @GetMapping("/{id}")
    public Result findOneById(@PathVariable @ApiParam("ID") Integer id) {
        return Result.success(roleMenuService.getById(id));
    }

    @ApiOperation(value = "分页查询RoleMenu")
    @GetMapping("/page")
    public Result findPage(@RequestParam @ApiParam("页码") Integer pageNum,
                           @RequestParam @ApiParam("页大小") Integer pageSize) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(roleMenuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation(value = "通过roleId获取全部menuIds")
    @GetMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(@RequestParam @ApiParam("角色ID") Integer roleId) {
        return Result.success(roleMenuService.findMenuIdsByRoleId(roleId));
    }

    @ApiOperation(value = "添加角色菜单权限")
    @PostMapping("/addMenuIdsByRoleId")
    public Result addMenuIdsByRoleId(@RequestBody @ApiParam("MenuIds 数组") AddMenuIdsByRoleIdDTO dto) {
        //删除所有再添加
        return Result.success(roleMenuService.addMenuIdsByRoleId(dto));
    }
}
